package com.qiwx.pigeon.activity.home;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.andview.refreshview.XRefreshView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.DBSqlUtil;
import com.qiwx.pigeon.Utils.MyToast;
import com.qiwx.pigeon.adapter.home.CageChangeAdapter;
import com.qiwx.pigeon.app.AppContext;
import com.qiwx.pigeon.base.BaseActivity;
import com.qiwx.pigeon.data.db.CageModel;
import com.qiwx.pigeon.data.params.CageParam;
import com.qiwx.pigeon.data.vo.CageBean;
import com.qiwx.pigeon.data.vo.CageBeanVo;
import com.qiwx.pigeon.data.vo.CageRemindBean;
import com.qiwx.pigeon.data.vo.WareHouseBean;
import com.qiwx.pigeon.data.vo.pageinfo.PageInfo;
import com.qiwx.pigeon.fragment.manager.CagemanagerFragment;
import com.qiwx.pigeon.widget.ArrowView;
import com.qiwx.pigeon.widget.itemderection.LineItemDecoration;

import org.angmarch.views.NiceSpinner;
import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @description
 * @Author qiwx
 * @Date 2020-04-06 10:31
 * 调窝
 **/
public class CageChangeActivity extends BaseRemindActivity {
    CageParam cageParam;
    List<CageBeanVo> data = new ArrayList<>();
    int current_pos = -1;


//    @Override
//    public int getLayoutId() {
//        return R.layout.activity_cage_change;
//    }

    @Override
    public void initAfterOnCreate() {
        cageParam = new CageParam();
        cageParam.setPageSize(pageSize);
        cageParam.setF_id(AppContext.getFarmId(this));
        initView();
        //wh_id = getIntent().getIntExtra("wh_id", 0);
        // xRefreshView.startRefresh();
        cageParam.setPageSize(pageSize);
        cageParam.setIs_adjust(1);
        //loadData(true);
    }

    //    //查询总数
//    public void queryAllAmount() {
//        allCount = LitePal.where("wh_id=? and is_adjust=?", wh_id + "", "1").count(CageModel.class);
//        txtAmount.setText(allCount + "");
//    }
    @Override
    public void loadData(boolean isRefresh) {
        this.isFresh = isRefresh;
        if (isRefresh) {
            pageNo = 1;
            data.clear();
        } else {
            pageNo++;
        }
        cageParam.setPageNum(pageNo);
        cageParam.setWh_id(wh_id);

        mPresenter.queryCageChangeByPage(cageParam);
//        int offset = (pageNo - 1) * pageSize;
////        String sql="select * from pigeonpairmodel where wh_id="+wh_id;
//        List<CageModel> list = LitePal
//                .limit(pageSize)
//                .offset(offset)
//                .order("number")
//                .where("wh_id=? and is_adjust=?", wh_id + "", "1").find(CageModel.class);
//        if (list != null && list.size() > 0) {
//            for (int i = 0; i < list.size(); i++) {
//                CageModel model = list.get(i);
//                CageRemindBean bean = new CageRemindBean();
//                bean.setC_id(model.getId());
//                bean.setC_number(model.getNumber());
//                bean.setStatus(model.isIs_adjust() ? 1 : 0);
//                data.add(bean);
//            }
//        }

    }

    @Override
    public BaseQuickAdapter getAdapter() {
        adapter = new CageChangeAdapter(R.layout.adapter_remind_common_list_item, data);
        return adapter;
    }

    @Override
    public int getSize() {
        return data.size();
    }

    @Override
    public int addListViewHead() {
        return R.layout.cage_remind_headview;
    }

    @Override
    public void initView() {
        super.initView();
        titleReturn.changeArrowLeft();
        titleContent.setText("待调窝鸽笼详情列表");
        txtAmountName.setText("待调窝合计(个)：");
        if (adapter != null && adapter instanceof CageChangeAdapter) {
            ((CageChangeAdapter) adapter).setIPigeonPairInterface((pos, c_id, methoId) -> {
                changeCage(pos, c_id);
            });
        }

    }

    //调窝
    public void changeCage(int pos, int c_id) {
        current_pos = pos;
        CageParam cageParam = new CageParam();
        cageParam.setC_id(c_id);
        cageParam.setIs_adjust(0);
        mPresenter.updateCageIsAdjust(cageParam);
    }


    @Override
    public void onQueryCageSuccess(PageInfo<CageBeanVo> bean) {
        if (bean != null && bean.getList() != null) {
            data.addAll(bean.getList());
        }
        super.onQueryCageSuccess(bean);

    }

    @Override
    public void onUpdateCageStatusSuccess(int type) {
        MyToast.showShort("操作成功");
        if (current_pos >= 0) {
            allCount = allCount - 1;
            txtAmount.setText(allCount + "");
            data.remove(current_pos);
            adapter.notifyDataSetChanged();
            current_pos = -1;
        }

    }
}
